home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / vg-2.03 / video / vbios.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-03  |  4.2 KB  |  264 lines

  1. /*
  2.  * Copyright (C) 1990-1992 Michael Davidson.
  3.  * All rights reserved.
  4.  *
  5.  * Permission to use, copy, modify, and distribute this software
  6.  * and its documentation for any purpose and without fee is hereby
  7.  * granted, provided that the above copyright notice appear in all
  8.  * copies and that both that copyright notice and this permission
  9.  * notice appear in supporting documentation.
  10.  *
  11.  * This software is provided "as is" without express or implied warranty.
  12.  */
  13.  
  14. /*
  15.  * vbios.c    - interface to video bios functions
  16.  */
  17. #include    <unistd.h>
  18.  
  19. #include    "v86mode.h"
  20. #include    "vbios.h"
  21.  
  22. #define    MEMFILE    "/dev/mem"
  23.  
  24. STATIC int    VBiosMemLoad(char *file, long off, long virt, long len);
  25. STATIC int    VBiosChecksum(unsigned char *b);
  26. STATIC void    VBiosINT10Init();
  27.  
  28. unsigned char    *VideoBiosAddress;
  29.  
  30. /*
  31.  * VBiosInit()    - initialise video BIOS
  32.  */
  33. int
  34. VBiosInit()
  35. {
  36.     int            r;
  37.     unsigned long    v;
  38.  
  39.     /*
  40.      * initialise the V86 mode environment
  41.      */
  42.     if ((r = V86Init()) != 0)
  43.     return r;
  44.     
  45.     /*
  46.      * map the display adaptor memory
  47.      */
  48.     if ((r = V86MemMap((paddr_t)0xa0000, (caddr_t)0xa0000, 0x20000)) != 0)
  49.     return r;
  50.     
  51.     /*
  52.      * enable i/o to the display adaptor
  53.      */
  54.     if ((r = V86IOEnable(0x3b0, 0x30)) != 0)
  55.     return r;
  56.     
  57.     /*
  58.      * load the real-mode interrupt vectors and BIOS data area
  59.      */
  60.     if ((r = VBiosMemLoad(MEMFILE, 0x0, 0x0, 0x1000)) != 0)
  61.     return r;
  62.  
  63.     /*
  64.      * use the INT 10 vector to locate the video BIOS
  65.      */
  66.     v = ((unsigned long *) 0)[0x10];
  67.     v = (v & 0xffff0000) >> 12;
  68.  
  69.     /*
  70.      * load the video BIOS
  71.      */
  72.     if ((r = VBiosMemLoad(MEMFILE, v, v, 0x8000)) != 0)
  73.     return r;
  74.  
  75.     /*
  76.      * checksum the video BIOS
  77.      */
  78.     if ((r = VBiosChecksum((unsigned char *)v)) != 0)
  79.     return r;
  80.  
  81.     VideoBiosAddress = (unsigned char *) v;
  82.  
  83.     /*
  84.      * load the system BIOS
  85.      */
  86.     if ((r = VBiosMemLoad(MEMFILE, 0xf0000, 0xf0000, 0x10000)) != 0)
  87.     return r;
  88.  
  89.  
  90.     VBiosINT10Init();
  91.  
  92.     return 0;
  93. }
  94.  
  95. /*
  96.  * VBiosMemLoad() - load V86 address space from a file
  97.  */
  98. STATIC int
  99. VBiosMemLoad(
  100.     char    *file,
  101.     long    off,
  102.     long    virt,
  103.     long    len
  104.     )
  105. {
  106.     int        fd;
  107.     extern long    lseek(int, long, int);
  108.  
  109.     if ((fd = open(file, 0)) < 0)
  110.     return -1;
  111.  
  112.     if (lseek(fd, off, 0) != off || read(fd, (char *) virt, len) != len)
  113.     {
  114.     close(fd);
  115.     return -1;
  116.     }
  117.  
  118.     close(fd);
  119.  
  120.     return 0;
  121. }
  122.  
  123. /*
  124.  * VBiosChecksum()    - check the integrity of the video BIOS
  125.  */
  126. STATIC int
  127. VBiosChecksum(
  128.     unsigned char    *b
  129.     )
  130. {
  131.     int        c;
  132.     int        n;
  133.  
  134.     if (b[0] != 0x55 || b[1] != 0xaa)
  135.     return -1;
  136.  
  137.     n    = b[2] * 512;
  138.     c    = 0;
  139.     while (--n >= 0)
  140.     c += *b++;
  141.  
  142.     return (c & 0xff);
  143. }
  144.  
  145. STATIC word_t    vbios_cs;
  146. STATIC word_t    vbios_ip;
  147. STATIC word_t    vbios_ss;
  148. STATIC word_t    vbios_sp;
  149.  
  150. STATIC void
  151. VBiosINT10Init()
  152. {
  153.     byte_t    *p;
  154.  
  155.     vbios_cs    = VBIOS_SEG;
  156.     vbios_ip    = 0;
  157.     vbios_ss    = VBIOS_SEG;
  158.     vbios_sp    = 0xfffe;
  159.  
  160.     p = (byte_t *) (vbios_cs << 4);
  161.  
  162.     /*
  163.      * put an INT 10 followed by a HLT instruction at the start of the segment
  164.      */
  165.     p[0]    = 0xcd;
  166.     p[1]    = 0x10;
  167.     p[2]    = 0xf4;
  168.  
  169.     p[0xfffe]    = 0;
  170.     p[0xffff]    = 0;
  171. }
  172.  
  173. void
  174. VBiosINT10()
  175. {
  176.     register struct tss386    *t = v86tss;
  177.     int                r;
  178.  
  179.     CS(t)    =    vbios_cs;
  180.     IP(t)    =    vbios_ip;
  181.     SS(t)    =    vbios_ss;
  182.     SP(t)    =    vbios_sp;
  183.  
  184.     if ((r = V86Run()) != ERR_V86_HALT)
  185.     fatal(0, "V86 mode error: %s", V86ErrorMessage(r));
  186. }
  187.  
  188. void *
  189. VBiosGetFontInfo(
  190.     int        code
  191.     )
  192. {
  193.     register struct tss386    *t = v86tss;
  194.  
  195.     AX(t)    = 0x1130;
  196.     BL(t)    = 0;
  197.     BH(t)    = (byte_t) code;
  198.  
  199.     VBiosINT10();
  200.  
  201.     return (void *) ((ES(t) << 4) | BP(t));
  202. }
  203.  
  204. int
  205. VBiosSetMode(
  206.     int        mode
  207.     )
  208. {
  209.     register struct tss386    *t = v86tss;
  210.  
  211.     AH(t)    = 0;
  212.     AL(t)    = (byte_t) mode;
  213.  
  214.     VBiosINT10();
  215.  
  216.     return AX(t);
  217. }
  218.  
  219. int
  220. VBiosGetMode()
  221. {
  222.     register struct tss386    *t = v86tss;
  223.  
  224.     AH(t)    = 0x0f;
  225.  
  226.     VBiosINT10();
  227.  
  228.     return AL(t);
  229. }
  230.  
  231. int
  232. VBiosDisableBlink()
  233. {
  234.     register struct tss386    *t = v86tss;
  235.  
  236.     AH(t)    = 0x10;
  237.     AL(t)    = 0x03;
  238.  
  239.     VBiosINT10();
  240.  
  241.     return 0;
  242. }
  243.  
  244. int
  245. VBiosSetCursorPosition(
  246.     int        x,
  247.     int        y
  248.     )
  249. {
  250.     register struct tss386    *t = v86tss;
  251.  
  252.     AH(t)    = 0x02;
  253.     AL(t)    = 0x00;
  254.     BX(t)    = 0;
  255.     CX(t)    = 0;
  256.     DH(t)    = (byte_t) y;
  257.     DL(t)    = (byte_t) x;
  258.  
  259.     VBiosINT10();
  260.  
  261.     return 0;
  262. }
  263.  
  264.